package com.api.syh56.shengyunhui.controller;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Coupon;
import com.api.syh56.shengyunhui.entity.CouponDet;
import com.api.syh56.shengyunhui.entity.Quote;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.vo.CouponVO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.CouponDetMapper;
import com.api.syh56.shengyunhui.service.CouponDetService;
import com.api.syh56.shengyunhui.service.CouponService;
import com.api.syh56.shengyunhui.service.QuoteService;
import com.api.syh56.shengyunhui.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import static com.baomidou.mybatisplus.extension.toolkit.Db.getById;

/**
 * 优惠券
 */
@Slf4j
@RestController
@RequestMapping("coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponDetService couponDetService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuoteService quoteService;



//    @PostMapping("lock")
//    public CommonResult<Object> getLock(@RequestBody Long qid){
//
//        Quote quoteDB = quoteService.getById(qid);
////        if(quoteDB.getStatus().equals(Constants.QuoteStatus.CONFIRMING)){
////            throw new BaseException("您的订单正在确认报价");
////        }else {
//            Long cuoId = quoteDB.getCuoId();
//            if (cuoId != null) {
//                QueryWrapper<CouponDet> wrapper = new QueryWrapper<>();
//                wrapper.lambda().eq(CouponDet::getId,cuoId);
//                CouponDet cou = couponDetService.getOne(wrapper);
//                if (cou.getCouponStatus().equals(Constants.couponStatus.IN_USE)) {
//                    throw new BaseException("您的优惠券已被使用");
//                }else {
//                    cou.setCouponStatus(Constants.couponStatus.IN_USE);
//                    couponDetService.updateById(cou);
//                }
//            }
//            //修改报价状态为正在报价
////            quoteDB.setStatus(Constants.QuoteStatus.CONFIRMING);
////            quoteService.updateById(quoteDB);
////        }
//        return CommonResult.success(null);
//    }


    /**
     * 获取实付金额
     * @param qid
     * @param couponId
     * @return
     */
    @GetMapping("getPay")
    public CommonResult<Object> updateStatus(Long qid, Long couponId){
        log.info("getPay:"+couponId);
        BigDecimal PayAmount = couponDetService.getPayAmount(qid,couponId);

        return CommonResult.success(PayAmount);
    }

    /**
     * 解锁优惠券状态/报价状态
     * @param qid
     * @return
     */
    @GetMapping("restore")
    public CommonResult<Object> restore(Long qid){
        log.info("调用恢复状态接口");
        couponDetService.restore(qid);
//        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
//        int couponNum = couponDetService.getCouponDetNum(uid);
//        User user = userService.getById(uid);
//        user.setCouponNum(couponNum);
//        userService.updateById(user);
        return CommonResult.success(null);
    }

    /**
     * 获取优惠券列表
     * @param pageNum
     * @param pageSize
     * @param couponStatus   优惠券状态
     * @return
     */
    @GetMapping("getCoupon")
    public CommonResult<Object> getCoupon(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize,
                                          Short couponStatus){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<CouponVO> couponDetPage = couponDetService.getCoupon(uid,pageNum,pageSize,couponStatus);
        return CommonResult.success(couponDetPage);
    }


    /**
     * 增加优惠券测试
     * @return
     */
    @PostMapping("insert")
    private CommonResult<Object> add(){
//        couponService.insert();
        couponDetService.insert();
        String string = BaseUserInfo.get(UserInfoEnu.id.getCode());
        Long aLong = Long.valueOf(string);
        QueryWrapper<CouponDet> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(CouponDet::getUid,aLong)
                .eq(CouponDet::getCouponStatus,Constants.couponStatus.EFFECTIVE);
        List<CouponDet> list = couponDetService.list(wrapper);
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.lambda()
                .eq(User::getId,aLong)
                .set(User::getCouponNum,list.size());
        userService.update(userUpdateWrapper);
        return CommonResult.success(null);
    }

    /**
     * 查看有效优惠券数量
     * @return
     */
    @GetMapping("couNum")
    private CommonResult<Object> getNum(){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        int couponNum = couponDetService.getCouponDetNum(uid);
        return CommonResult.success(couponNum);
    }
}
